﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  UtilityString   
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  : 
 * Created Time :  2013/5/22 0:24:49
 * Update Man   :  Joey Liang
 * Update Time  :  2013/5/22 0:24:49
************************************************************************/

using System;

namespace JoeyLibs.Utility
{
    /// <summary>
    /// 字符串帮助类
    /// </summary>
    public class UtilityString
    {
        /// <summary>
        /// 将字符串按照分隔符拆分为字符数组
        /// </summary>
        /// <param name="str">需拆分的字符串</param>
        /// <param name="separate">分隔符，默认为“,”</param>
        /// <param name="clearEmpty">是否去掉空项</param>
        /// <param name="isTrim">是否去掉每项的空格</param>
        public static string[] SplitToArray(string str, char separate, bool clearEmpty = true, bool isTrim = false)
        {
            return SplitToArray(str, separate.ToString(), clearEmpty, isTrim);
        }

        /// <summary>
        /// 将字符串按照分隔符拆分为字符数组
        /// </summary>
        /// <param name="str">需拆分的字符串</param>
        /// <param name="separate">分隔符，默认为“,”</param>
        /// <param name="clearEmpty">是否去掉空项</param>
        /// <param name="isTrim">是否去掉每项的空格</param>
        public static string[] SplitToArray(string str, string separate = ",", bool clearEmpty = true, bool isTrim = false)
        {
            string[] array = null;
            if (string.IsNullOrEmpty(str))
            {
                return array;
            }

            if (string.IsNullOrEmpty(separate))
            {
                return new string[] { str };
            }

            array = str.Split(separate.ToCharArray(), 
                clearEmpty ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
            if (isTrim)
            {
                array = TrimArray(array);
            }

            return array;
        }

        /// <summary>
        /// 去掉字符串数组所有项的空格并返回
        /// </summary>
        public static string[] TrimArray(params string[] array)
        {
            if (array == null)
            {
                return null;
            }

            for (int i = 0; i < array.Length; i++)
            {
                if (!string.IsNullOrEmpty(array[i]))
                {
                    array[i] = array[i].Trim();
                }
            }

            return array;
        }
    }
}
